/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.Vector;

import Models.HoaDon_Model;
import Beans.NhanVien;
import Models.NhatKyHT_Model;

/**
 *
 * @author Venus
 */
public class HoaDon_Controller extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            int langid = (Integer) session.getAttribute("langid");
            String action = request.getParameter("action");
            if (action.equals("Show")) {
                String item = request.getParameter("item");
                HoaDon_Model m = new HoaDon_Model();
                Vector v = new Vector();
                v = m.LayDSHoaDon(langid);
                request.setAttribute("DSHD", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/hoadon.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("DeleteHD")) {
                String sohd = request.getParameter("sohd");
                String item = request.getParameter("item");
                String SoHoaDon[] = sohd.split("-");
                int j = 0;
                while (j < SoHoaDon.length) {
                    HoaDon_Model.Delete(SoHoaDon[j]);
                    j = j + 1;
                }
                HoaDon_Model m = new HoaDon_Model();
                Vector v = new Vector();
                v = m.LayDSHoaDon(langid);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(4, nv.getMaNV(), "bill");
                request.setAttribute("DSHD", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/hoadon.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortNgayGio")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                HoaDon_Model hdm = new HoaDon_Model();
                Vector v = new Vector();
                v = hdm.SortNgayGio(langid, order);
                request.setAttribute("DSHD", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortbill.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSoHD")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                HoaDon_Model hdm = new HoaDon_Model();
                Vector v = new Vector();
                v = hdm.SortSoHD(langid, order);
                request.setAttribute("DSHD", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortbill.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortKhuVucHD")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                HoaDon_Model hdm = new HoaDon_Model();
                Vector v = new Vector();                
                v = hdm.SortKhuVucHD(langid, order);
                request.setAttribute("DSHD", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortbill.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortBanHD")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                HoaDon_Model hdm = new HoaDon_Model();
                Vector v = new Vector();
                v = hdm.SortBanHD(langid, order);
                request.setAttribute("DSHD", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "4");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortbill.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortTongCongHD")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                HoaDon_Model hdm = new HoaDon_Model();
                Vector v = new Vector();
                v = hdm.SortTongCongHD(langid, order);
                request.setAttribute("DSHD", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "5");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortbill.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("Search")) {
                String hoadon = request.getParameter("hoadon");
                String khuvuc = request.getParameter("khuvuc");
                String ban = request.getParameter("ban");
                String tongcong = request.getParameter("tongcong");
                String item = request.getParameter("item");
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                HoaDon_Model hdm = new HoaDon_Model();
                Vector v = new Vector();
                v = hdm.Search( tungay , tugio, tuphut , denngay , dengio , denphut , hoadon, khuvuc, ban, tongcong, langid);
                request.setAttribute("hoadon", hoadon);
                request.setAttribute("khuvuc", khuvuc);
                request.setAttribute("ban", ban);
                request.setAttribute("tongcong", tongcong);
                request.setAttribute("item", item);
                request.setAttribute("DSHD", v);
                request.setAttribute("tungay", tungay);
                request.setAttribute("tugio", tugio);
                request.setAttribute("tuphut", tuphut);
                request.setAttribute("denngay", denngay);
                request.setAttribute("dengio", dengio);
                request.setAttribute("denphut", denphut);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssearchbill.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchNgayGio")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                String hoadon = request.getParameter("hoadon");
                String khuvuc = request.getParameter("khuvuc");
                String ban = request.getParameter("ban");
                String tongcong = request.getParameter("tongcong");
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                HoaDon_Model hdm = new HoaDon_Model();
                Vector v = new Vector();                
                v = hdm.SortSearchNgayGio( tungay , tugio, tuphut , denngay , dengio , denphut , hoadon, khuvuc, ban, tongcong, langid, order);
                request.setAttribute("hoadon", hoadon);
                request.setAttribute("khuvuc", khuvuc);
                request.setAttribute("ban", ban);
                request.setAttribute("tongcong", tongcong);
                request.setAttribute("item", item);
                request.setAttribute("DSHD", v);
                request.setAttribute("tungay", tungay);
                request.setAttribute("tugio", tugio);
                request.setAttribute("tuphut", tuphut);
                request.setAttribute("denngay", denngay);
                request.setAttribute("dengio", dengio);
                request.setAttribute("denphut", denphut);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchbill.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchSoHD")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                String hoadon = request.getParameter("hoadon");
                String khuvuc = request.getParameter("khuvuc");
                String ban = request.getParameter("ban");
                String tongcong = request.getParameter("tongcong");
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                HoaDon_Model hdm = new HoaDon_Model();
                Vector v = new Vector();
                v = hdm.SortSearchSoHD( tungay , tugio, tuphut , denngay , dengio , denphut , hoadon, khuvuc, ban, tongcong, langid, order);
                request.setAttribute("hoadon", hoadon);
                request.setAttribute("khuvuc", khuvuc);
                request.setAttribute("ban", ban);
                request.setAttribute("tongcong", tongcong);
                request.setAttribute("item", item);
                request.setAttribute("DSHD", v);
                request.setAttribute("tungay", tungay);
                request.setAttribute("tugio", tugio);
                request.setAttribute("tuphut", tuphut);
                request.setAttribute("denngay", denngay);
                request.setAttribute("dengio", dengio);
                request.setAttribute("denphut", denphut);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchbill.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchKhuVucHD")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                String hoadon = request.getParameter("hoadon");
                String khuvuc = request.getParameter("khuvuc");
                String ban = request.getParameter("ban");
                String tongcong = request.getParameter("tongcong");
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                HoaDon_Model hdm = new HoaDon_Model();
                Vector v = new Vector();
                v = hdm.SortSearchKhuVucHD( tungay , tugio, tuphut , denngay , dengio , denphut , hoadon, khuvuc, ban, tongcong, langid, order);
                request.setAttribute("hoadon", hoadon);
                request.setAttribute("khuvuc", khuvuc);
                request.setAttribute("ban", ban);
                request.setAttribute("tongcong", tongcong);
                request.setAttribute("item", item);
                request.setAttribute("DSHD", v);
                request.setAttribute("tungay", tungay);
                request.setAttribute("tugio", tugio);
                request.setAttribute("tuphut", tuphut);
                request.setAttribute("denngay", denngay);
                request.setAttribute("dengio", dengio);
                request.setAttribute("denphut", denphut);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchbill.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchBanHD")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                String hoadon = request.getParameter("hoadon");
                String khuvuc = request.getParameter("khuvuc");
                String ban = request.getParameter("ban");
                String tongcong = request.getParameter("tongcong");
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                HoaDon_Model hdm = new HoaDon_Model();
                Vector v = new Vector();
                v = hdm.SortSearchBanHD( tungay , tugio, tuphut , denngay , dengio , denphut , hoadon, khuvuc, ban, tongcong, langid, order);
                request.setAttribute("hoadon", hoadon);
                request.setAttribute("khuvuc", khuvuc);
                request.setAttribute("ban", ban);
                request.setAttribute("tongcong", tongcong);
                request.setAttribute("item", item);
                request.setAttribute("DSHD", v);
                request.setAttribute("tungay", tungay);
                request.setAttribute("tugio", tugio);
                request.setAttribute("tuphut", tuphut);
                request.setAttribute("denngay", denngay);
                request.setAttribute("dengio", dengio);
                request.setAttribute("denphut", denphut);
                request.setAttribute("order", order);
                request.setAttribute("test", "4");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchbill.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchTongCongHD")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                String hoadon = request.getParameter("hoadon");
                String khuvuc = request.getParameter("khuvuc");
                String ban = request.getParameter("ban");
                String tongcong = request.getParameter("tongcong");
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                HoaDon_Model hdm = new HoaDon_Model();
                Vector v = new Vector();
                v = hdm.SortSearchTongCongHD( tungay , tugio, tuphut , denngay , dengio , denphut , hoadon, khuvuc, ban, tongcong, langid, order);
                request.setAttribute("hoadon", hoadon);
                request.setAttribute("khuvuc", khuvuc);
                request.setAttribute("ban", ban);
                request.setAttribute("tongcong", tongcong);
                request.setAttribute("item", item);
                request.setAttribute("DSHD", v);
                request.setAttribute("tungay", tungay);
                request.setAttribute("tugio", tugio);
                request.setAttribute("tuphut", tuphut);
                request.setAttribute("denngay", denngay);
                request.setAttribute("dengio", dengio);
                request.setAttribute("denphut", denphut);
                request.setAttribute("order", order);
                request.setAttribute("test", "5");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchbill.jsp");
                rd.forward(request, response);
            }

        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
